<?php

namespace app\modules\api\controllers;

use yii;
use ac\controllers\BasicController;
use app\models\Star;
use app\models\GoodsCategory;
use app\models\GoodsCategorySearch;
use app\models\Goods;
use app\models\GoodsSearch;
use app\models\Experts;
use ac\component\tool\Tool;
use ac\component\exception\ErrMap;


class GoodsController extends BasicController
{

    /*商品分类*/
    public function actionCate()
    {
        // P($this->get);
        $search = (new GoodsCategorySearch())->search($this->get);
        if($search === false){//验证搜索参数不通过
            $count = 0;
            $goodsCategoryList = array();
        }else{
            if(isset($this->get['type']) and $this->get['type'] == 'ele'){
                $flag = 'ele';
            }else{
                $flag = '';
            }
            if(isset( $this->get['parentid'] )){
                $parentid = $this->get['parentid'];
            }else{
                $parentid = 0;
            }
            // P($search);
            $goodsCategoryList = GoodsCategory::getTreeList($search, $flag, $parentid);
            // P($goodsCategoryList);
        }
        return Tool::showRes([0, '获取成功'], $goodsCategoryList);
    }

    /*商品列表*/
    public function actionList()
    {
        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        if(isset($this->get['pagesize'])){
            $pageSize = intval($this->get['pagesize']?$this->get['pagesize']:Tool::getPageSize());
        }else{
            $pageSize = Tool::getPageSize();
        }
        // P($this->get);

        /* 如果有关键词 需要查询是否有分类名匹配 */
        if(isset($this->get['keyword'])) {
            $goodsCategory = GoodsCategory::find()->select("catid")
                        ->where('name = :name', [':name' => $this->get['keyword']])
                        ->one();
            if(!is_null($goodsCategory)) {
                $this->get["catid"] = $goodsCategory->catid;
            }
        }

        $search = (new GoodsSearch())->search($this->get);
        if($search === false){//验证搜索参数不通过
            $count = 0;
            $goodsList = array();
        }else{
            $count = $search->count();
            $goodsList = $search
                          ->offset($pageSize*($currPage-1))->limit($pageSize)
                          ->orderBy([
                                'add_time' => SORT_DESC
                            ])
                          // ->createCommand()->getRawSql();
                          ->asArray()->all();
        }
        // P($goodsList);
        $res = array(
            'list' => $goodsList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }

    /*商品详情*/
    public function actionDetail()
    {
        // P($this->get);
        $catid = isset($this->get['catid'])?$this->get['catid']:'';
        $goods_id = isset($this->get['id'])?$this->get['id']:'';
        if($catid or $goods_id){
            if($goods_id){
                $goods = Goods::find()
                        ->where('goods_id = :cid and state = 1', [':cid' => $goods_id])
                        ->asArray()->one();
            }else{//说明是单页,获取次栏目最大的ID数据
                $goods = Goods::find()
                        ->where('catids = :cid and state = 1', [':cid' => $catid])
                        ->orderBy(['goods_id' => SORT_DESC])
                        ->asArray()->one();
                $goods_id = $goods['goods_id'];
                // P($goods_id);
            }
            // P($goods);
            if(!is_null($goods)){
                if(!empty($goods['preview'])){
                    $goods['preview'] = explode(',', $goods['preview']);
                }else{
                    $goods['preview'] = [];
                }
                $goods["code"] = str_pad($goods['goods_id'], 6, "0", STR_PAD_LEFT);

                $goods["star"] = false;
                /* 如果已经登录了，验证是否收藏 */
                if(Yii::$app->session->get('loginInfo') != null){
                    $this->userInfo = Yii::$app->session->get('loginInfo');
                    $mid = $this->userInfo->member_id;
                    $star = Star::find()
                            ->where('goods_id = :id and member_id = :mid', [':id' => $goods_id, ':mid' => $mid])
                            ->one();
                    if(!is_null($star)) {
                        $goods["star"] = true;
                    }
                };

                $data = array(
                    'goods' => $goods
                );

                return Tool::showRes([0, '获取成功'], $data);
            }else{
                $data = array(
                    'goods' => ''
                );
                return Tool::showRes([0, '获取成功'], $data);
            };
        }

        return Tool::showRes(ErrMap::getError(10005));

    }

}
